<template>
  <div class="login">
    <div class="wrapper">
      <div v-if="!isLogin" class="register-box">
        <div class="header">面经注册</div>
        <div class="username">
          <div>用户名</div>
          <input
            type="text"
            v-model="registerUser.username"
            placeholder="请填写用户名"
          />
        </div>
        <div class="password">
          <div>密码</div>
          <input
            type="password"
            v-model="registerUser.password"
            placeholder="请填写密码"
          />
        </div>
        <button @click="registerUser()" class="re-done">注册</button>
        <div @click="isLogin = true" class="register">
          <a href="#">有账号，去登陆</a>
        </div>
      </div>

      <div v-else class="login-box">
        <div class="header">面经登录</div>
        <div class="username">
          <div>用户名</div>
          <input
            type="text"
            v-model="loginUser.username"
            placeholder="用户名"
          />
        </div>
        <div class="password">
          <div>密码</div>
          <input
            type="password"
            v-model="loginUser.password"
            placeholder="密码"
          />
        </div>
        <button
          @click="$router.push({ path: '/layout/article' })"
          class="re-done"
        >
          登录
        </button>
        <div @click="isLogin = false" class="login">
          <a href="#">注册账号</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "HmLogin",
  data() {
    return {
      isLogin: false,
      loginUser: {
        username: "",
        password: "",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //注册账号
    async registerUser() {
      const res = await axios({
        method: "post",
        url: "http://interview-api-t.itheima.net/h5/user/register",
        data: {
          username: this.registerUser.username,
          password: this.registerUser.password,
        },
      });
      if (res.data.code === 10000) {
        alert("注册成功");
      } else {
        alert("注册失败");
      }
    },
    async getList() {
      const res = await axios({
        method: "post",
        url: "http://interview-api-t.itheima.net/h5/user/login",
        data: { username: "alicia", password: "123456" },
        Authorization: "Bearer " + res.data.result.token,
      });
      if (res.data.code === 10000) {
        alert("登录成功");
      } else {
        alert("登录失败");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.register-box {
  width: 100%;

  .header {
    width: 100%;
    height: 50px;
    text-align: center;
    font-size: 18px;
    line-height: 50px;
  }
  .username,
  .password {
    width: 100%;
    height: 60px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    text-align: left;
  }
  .re-done {
    width: 100%;
    height: 40px;
    background-color: green;
    text-align: center;
    line-height: 40px;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    border-radius: 5px;
    align-items: center;
  }
  a {
    float: right;
    margin-top: 10px;
    color: #084192;
    margin-right: 10px;
  }
}

.login-box {
  width: 100%;

  .header {
    width: 100%;
    height: 50px;
    text-align: center;
    font-size: 18px;
    line-height: 50px;
  }
  .username,
  .password {
    width: 100%;
    height: 60px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    text-align: left;
  }
  .re-done {
    width: 100%;
    height: 40px;
    background-color: rgb(228, 79, 10);
    text-align: center;
    line-height: 40px;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    border-radius: 5px;
    align-items: center;
  }
  a {
    float: right;
    margin-top: 10px;
    color: #084192;
    margin-right: 10px;
  }
}
</style>
